package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.MyForumBean;
import mamikeji.liaowa.ui.activity.circle.PreviewCircleImgActivity;

/**
 * Created by 廉冲-android开发 on 2017/9/12.
 */
public class MyForumAdapter extends BaseAdapter {
    List<MyForumBean.DataEntity> mList = new ArrayList<>();
    LayoutInflater inflater;
    Context context;
    OnItemClickListener mOnItemClickListener;
    ViewHolder viewHolder;
    public boolean bool_delete = false;

    public MyForumAdapter() {
    }

    public MyForumAdapter(List<MyForumBean.DataEntity> mList, Context context) {
        this.mList = mList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onZanClick(int fc_id, int position);

        void onMSGClick(int fc_id, int position);

        void onDeleteClick(int fc_id, int position);
    }

    public void setList(List<MyForumBean.DataEntity> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_my_forum, null);
            viewHolder = new ViewHolder();

            viewHolder.textView_my_forum_nickname = (TextView) convertView.findViewById(R.id.textView_my_forum_nickname);
            viewHolder.textView_my_forum_position = (TextView) convertView.findViewById(R.id.textView_my_forum_position);
            viewHolder.textView_my_forum_title = (TextView) convertView.findViewById(R.id.textView_my_forum_title);
            viewHolder.textView_my_forum_content = (TextView) convertView.findViewById(R.id.textView_my_forum_content);
            viewHolder.textView_my_forum_zannum = (TextView) convertView.findViewById(R.id.textView_my_forum_zannum);
            viewHolder.textView_my_forum_recommend = (TextView) convertView.findViewById(R.id.textView_my_forum_recommend);
            viewHolder.textView_my_forum_browsenum = (TextView) convertView.findViewById(R.id.textView_my_forum_browsenum);
            viewHolder.textView_my_forum_creattime = (TextView) convertView.findViewById(R.id.textView_my_forum_creattime);

            viewHolder.imageView_my_forum_titleimg = (SimpleDraweeView) convertView.findViewById(R.id.imageView_my_forum_titleimg);
            viewHolder.imageView_my_forum_identity = (ImageView) convertView.findViewById(R.id.imageView_my_forum_identity);
            viewHolder.imageView_my_forum_img = (ImageView) convertView.findViewById(R.id.imageView_my_forum_img);
            viewHolder.imageView_my_forum_zannum = (ImageView) convertView.findViewById(R.id.imageView_my_forum_zannum);

            viewHolder.relativeLayout_my_forum_delete = (RelativeLayout) convertView.findViewById(R.id.relativeLayout_my_forum_delete);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final MyForumBean.DataEntity dataEntity = mList.get(position);

        viewHolder.imageView_my_forum_identity.setImageResource(dataEntity.getCertification().endsWith("") ? R.drawable.icon_renzheng_n : R.drawable.icon_renzheng_h);
        viewHolder.imageView_my_forum_zannum.setImageResource(dataEntity.getIs_praise() == 1 ? R.drawable.icon_dianzan_h : R.drawable.icon_dianzan_n);
        viewHolder.textView_my_forum_nickname.setText(dataEntity.getNickname());
        viewHolder.textView_my_forum_title.setText(dataEntity.getTopic_title());
        viewHolder.textView_my_forum_position.setText(dataEntity.getCertification());
        viewHolder.textView_my_forum_browsenum.setText(dataEntity.getBrowse_total() + "");
        viewHolder.textView_my_forum_recommend.setText(dataEntity.getMessage_total() + "");
        viewHolder.textView_my_forum_zannum.setText(dataEntity.getPraise_total() + "");

        if (dataEntity.getTopic_content() == null || dataEntity.getTopic_content().equals("")) {
            viewHolder.textView_my_forum_content.setVisibility(View.GONE);
        } else {
            viewHolder.textView_my_forum_content.setVisibility(View.VISIBLE);
            viewHolder.textView_my_forum_content.setText(dataEntity.getTopic_content());

            if (dataEntity.getTopic_content().length() >= 20) {
                viewHolder.textView_my_forum_content.setLines(2);
                viewHolder.textView_my_forum_content.setEllipsize(TextUtils.TruncateAt.END);
            }
        }
        viewHolder.textView_my_forum_creattime.setText(mistiming(dataEntity.getPublishdate()));
        viewHolder.imageView_my_forum_titleimg.setImageURI(Uri.parse(dataEntity.getPhoto()));

        String[] split2 = new String[]{};
        if (!dataEntity.getTopic_image().equals("") && !dataEntity.getTopic_image().equals("null") && dataEntity.getTopic_image() != null) {
            split2 = dataEntity.getTopic_image().split(",");
            viewHolder.imageView_my_forum_img.setVisibility(View.VISIBLE);
            Glide
                    .with(context)
                    .load(split2[0] + "?from=singlemessage")
                    .error(R.drawable.img_dongtaitu)
                    .into(viewHolder.imageView_my_forum_img);
        } else {
            viewHolder.imageView_my_forum_img.setVisibility(View.GONE);
        }

        final String[] finalSplit = split2;
        viewHolder.imageView_my_forum_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!bool_delete) {
                    if (finalSplit.length > 0) {
                        ArrayList<String> stringList = new ArrayList<String>();
                        stringList.add(finalSplit[0]);

                        Intent intent = new Intent(context, PreviewCircleImgActivity.class);
                        intent.putStringArrayListExtra("urlList", stringList);
                        intent.putExtra("position", 1);
                        context.startActivity(intent);
                    }
                }
            }
        });

        viewHolder.imageView_my_forum_zannum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!bool_delete) {
                    mOnItemClickListener.onZanClick(0, position);
                }
            }
        });

        viewHolder.relativeLayout_my_forum_delete.setVisibility(bool_delete ? View.VISIBLE : View.GONE);

        viewHolder.relativeLayout_my_forum_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onDeleteClick(0, position);
            }
        });
        return convertView;
    }

    public class ViewHolder {
        TextView textView_my_forum_nickname;
        TextView textView_my_forum_position;
        TextView textView_my_forum_title;
        TextView textView_my_forum_content;
        TextView textView_my_forum_zannum;
        TextView textView_my_forum_recommend;
        TextView textView_my_forum_browsenum;
        TextView textView_my_forum_creattime;

        SimpleDraweeView imageView_my_forum_titleimg;
        ImageView imageView_my_forum_identity;
        ImageView imageView_my_forum_img;
        ImageView imageView_my_forum_zannum;

        RelativeLayout relativeLayout_my_forum_delete;
    }

    //判断时间差：新闻发布时间与当前系统时间的差值：如果小于60分钟换算成分钟；大于60分钟，换算成小时；大于24小时，换算成天
    public String mistiming(String time) {
        String date1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());

        SimpleDateFormat CurrentTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        Date beginTime = null;
        try {
            beginTime = CurrentTime.parse(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date endTime = null;
        try {
            endTime = CurrentTime.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //进行判断
        Long shijianCha = beginTime.getTime() - endTime.getTime();
        int count = 0;
        if (shijianCha > 24 * 60 * 60 * 1000) {
            count = (int) (shijianCha / (24 * 60 * 60 * 1000));
            return count + "天前";
        } else if (shijianCha > 60 * 60 * 1000) {
            count = (int) (shijianCha / (60 * 60 * 1000));
            return count + "小时前";
        } else {
            count = (int) (shijianCha / (60 * 1000));
            return count + "分钟前";
        }
    }
}